/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webidewar.jsf;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import org.WebIDE.ejb.entities.Replies;
import org.WebIDE.ejb.sessions.RepliesFacade;
import org.WebIDE.ejb.sessions.RepliesFacadeRemote;
import webidewar.jsf.util.JsfUtil;
import org.WebIDE.ejb.sessions.exceptions.NonexistentEntityException;
import webidewar.jsf.util.PagingInfo;

/**
 *
 * @author huy
 */
public class RepliesController {

    private RepliesFacadeRemote Facade;

    @EJB
    public void setFacade(RepliesFacadeRemote Facade) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.getApplication().getELResolver().setValue(facesContext.getELContext(), null, "repliesJpa", Facade);

        this.Facade = Facade;
    }

    public RepliesController() {
        //FacesContext facesContext = FacesContext.getCurrentInstance();
        //Facade = (RepliesFacade) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "repliesJpa");
        pagingInfo = new PagingInfo();
        converter = new RepliesConverter();
    }
    private Replies replies = null;
    private List<Replies> repliesItems = null;
    //private RepliesFacade Facade = null;
    private RepliesConverter converter = null;
    private PagingInfo pagingInfo = null;

    public PagingInfo getPagingInfo() {
        if (pagingInfo.getItemCount() == -1) {
            pagingInfo.setItemCount(Facade.getRepliesCount());
        }
        return pagingInfo;
    }

    public SelectItem[] getRepliesItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(Facade.findRepliesEntities(), false);
    }

    public SelectItem[] getRepliesItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(Facade.findRepliesEntities(), true);
    }

    public Replies getReplies() {
        if (replies == null) {
            replies = (Replies) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentReplies", converter, null);
        }
        if (replies == null) {
            replies = new Replies();
        }
        return replies;
    }

    public String listSetup() {
        reset(true);
        return "replies_list";
    }

    public String createSetup() {
        reset(false);
        replies = new Replies();
        return "replies_create";
    }

    public String create() {
        try {

            Facade.create(replies);
            JsfUtil.addSuccessMessage("Replies was successfully created.");
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return listSetup();
    }

    public String detailSetup() {
        return scalarSetup("replies_detail");
    }

    public String editSetup() {
        return scalarSetup("replies_edit");
    }

    private String scalarSetup(String destination) {
        reset(false);
        replies = (Replies) JsfUtil.getObjectFromRequestParameter("jsfcrud.currentReplies", converter, null);
        if (replies == null) {
            String requestRepliesString = JsfUtil.getRequestParameter("jsfcrud.currentReplies");
            JsfUtil.addErrorMessage("The replies with id " + requestRepliesString + " no longer exists.");
            return relatedOrListOutcome();
        }
        return destination;
    }

    public String edit() {
        String repliesString = converter.getAsString(FacesContext.getCurrentInstance(), null, replies);
        String currentRepliesString = JsfUtil.getRequestParameter("jsfcrud.currentReplies");
        if (repliesString == null || repliesString.length() == 0 || !repliesString.equals(currentRepliesString)) {
            String outcome = editSetup();
            if ("replies_edit".equals(outcome)) {
                JsfUtil.addErrorMessage("Could not edit replies. Try again.");
            }
            return outcome;
        }
        try {
            Facade.edit(replies);
            JsfUtil.addSuccessMessage("Replies was successfully updated.");
        } catch (NonexistentEntityException ne) {
            JsfUtil.addErrorMessage(ne.getLocalizedMessage());
            return listSetup();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return detailSetup();
    }

    public String destroy() {
        String idAsString = JsfUtil.getRequestParameter("jsfcrud.currentReplies");
        Integer id = new Integer(idAsString);
        try {
            Facade.destroy(id);
            JsfUtil.addSuccessMessage("Replies was successfully deleted.");
        } catch (NonexistentEntityException ne) {
            JsfUtil.addErrorMessage(ne.getLocalizedMessage());
            return relatedOrListOutcome();
        } catch (Exception e) {
            JsfUtil.ensureAddErrorMessage(e, "A persistence error occurred.");
            return null;
        }
        return relatedOrListOutcome();
    }

    private String relatedOrListOutcome() {
        String relatedControllerOutcome = relatedControllerOutcome();
        if (relatedControllerOutcome != null) {
            return relatedControllerOutcome;
        }
        return listSetup();
    }

    public List<Replies> getRepliesItems() {
        if (repliesItems == null) {
            getPagingInfo();
            repliesItems = Facade.findRepliesEntities(pagingInfo.getBatchSize(), pagingInfo.getFirstItem());
        }
        return repliesItems;
    }

    public String next() {
        reset(false);
        getPagingInfo().nextPage();
        return "replies_list";
    }

    public String prev() {
        reset(false);
        getPagingInfo().previousPage();
        return "replies_list";
    }

    private String relatedControllerOutcome() {
        String relatedControllerString = JsfUtil.getRequestParameter("jsfcrud.relatedController");
        String relatedControllerTypeString = JsfUtil.getRequestParameter("jsfcrud.relatedControllerType");
        if (relatedControllerString != null && relatedControllerTypeString != null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Object relatedController = context.getApplication().getELResolver().getValue(context.getELContext(), null, relatedControllerString);
            try {
                Class<?> relatedControllerType = Class.forName(relatedControllerTypeString);
                Method detailSetupMethod = relatedControllerType.getMethod("detailSetup");
                return (String) detailSetupMethod.invoke(relatedController);
            } catch (ClassNotFoundException e) {
                throw new FacesException(e);
            } catch (NoSuchMethodException e) {
                throw new FacesException(e);
            } catch (IllegalAccessException e) {
                throw new FacesException(e);
            } catch (InvocationTargetException e) {
                throw new FacesException(e);
            }
        }
        return null;
    }

    private void reset(boolean resetFirstItem) {
        replies = null;
        repliesItems = null;
        pagingInfo.setItemCount(-1);
        if (resetFirstItem) {
            pagingInfo.setFirstItem(0);
        }
    }

    public void validateCreate(FacesContext facesContext, UIComponent component, Object value) {
        Replies newReplies = new Replies();
        String newRepliesString = converter.getAsString(FacesContext.getCurrentInstance(), null, newReplies);
        String repliesString = converter.getAsString(FacesContext.getCurrentInstance(), null, replies);
        if (!newRepliesString.equals(repliesString)) {
            createSetup();
        }
    }

    public Converter getConverter() {
        return converter;
    }
}
